//
//  ClerkManagerListCell.m
//  tryshopping.boss
//
//  Created by Mr.Lee on 2017/3/24.
//  Copyright © 2017年 Reabam. All rights reserved.
//

#import "ClerkManagerListCell.h"
#import "StoreAssistantModel.h"

@interface ClerkManagerListCell () <UIGestureRecognizerDelegate>
// 头像
@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
// 名字
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
// 性别
@property (weak, nonatomic) IBOutlet UIImageView *sexImageView;
// 身份
@property (weak, nonatomic) IBOutlet UILabel *identityLabel;
// 状态
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;
// 手机号
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
// 操作状态
@property (weak, nonatomic) IBOutlet UIButton *statusBtn;
@property (weak, nonatomic) IBOutlet UILabel *stopLabel;

/** 尺寸 */
@property (nonatomic, assign) CGSize identityStrSize;
/** 更多操作按钮 */
@property (nonatomic, weak) UIButton *moreBtn;
/** 更多操作按钮 */
@property (nonatomic, weak) UIImageView *moreImage;
@property (nonatomic, weak) UIImageView *bigIV;

@end

@implementation ClerkManagerListCell

- (void)setModel:(StoreAssistantModel *)model{
    _model = model;
    
    // 设置头像
    NSString *urlStr = model.headImage;
    NSURL *url = [NSURL URLWithString:urlStr];
    [self.iconImageView sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"zhengfangxing"]];
    
    // 店员姓名
    self.nameLabel.text = model.userName;
    
    // 店员性别
    if ([model.sex isEqualToString:@"女"]) {
        self.sexImageView.image = [UIImage imageNamed:@"famale"];
    } else{
        self.sexImageView.image = [UIImage imageNamed:@"male"];
    }
    
    // 店员类型
    NSString *identityStr = model.userTypeName;
    CGSize identityStrSize = [identityStr boundingRectWithSize:CGSizeMake(ScreenWidth, 2000.f) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0]} context:nil].size;
    self.identityStrSize = identityStrSize;
    self.identityLabel.textAlignment = NSTextAlignmentCenter;
    self.identityLabel.font = [UIFont systemFontOfSize:12.0];
    self.identityLabel.text = identityStr;
    if ([model.userType isEqualToString:@"DianZhu"] || [model.userType isEqualToString:@"DianZhang"]) {  // 店长 店主
       self.identityLabel.backgroundColor = [UIColor colorWithHexString:@"deae08"];
    } else if ([model.userType isEqualToString:@"PtjDaoGou"]){  // 兼职导购
       self.identityLabel.backgroundColor = lanse3;
    } else {  // 导购
        self.identityLabel.backgroundColor = lanse;
    }
    self.identityLabel.textColor = [UIColor whiteColor];
    
    // 店员手机号码
    self.phoneLabel.text = model.userCode;
    
    
    // 操作状态
    if ([model.status isEqualToString:@"W"]) {  // w为待审核
        self.moreImage.hidden = YES;
        self.bigIV.hidden = YES;
        self.statusBtn.hidden = NO;
        self.statusLabel.text = @"待审核";
        self.stopLabel.hidden = YES;
        
    } else if ([model.status isEqualToString:@"Y"]){  //启用
        self.moreImage.hidden  = NO;
        self.bigIV.hidden = NO;
        self.statusBtn.hidden = YES;
        self.stopLabel.hidden = YES;
        self.statusLabel.hidden = YES;
        
        UIView *moreView = [[UIView alloc] init];
        moreView.backgroundColor = [UIColor clearColor]; // [UIColor colorWithWhite:0.f alpha:0.8]
        moreView.frame = CGRectMake(ScreenWidth, 0, 74*2, self.height);
        self.moreView = moreView;
        [self addSubview:moreView];
        
        // 中间按钮
        UIButton *middleBtn = [[UIButton alloc] init];
        middleBtn.frame = CGRectMake(0, 0, 74, 90);
        [middleBtn setTitle:@"停用" forState:UIControlStateNormal];
        [middleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        middleBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//        [middleBtn setBackgroundColor:[UIColor colorWithHexString:@"1294e6"]];
        [middleBtn setBackgroundColor:lanse4];
        [middleBtn addTarget:self action:@selector(middleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        middleBtn.alpha = 0.88;
        [self.moreView addSubview:middleBtn];
    
        // 右边按钮
        UIButton *rigthBtn = [[UIButton alloc] init];
        rigthBtn.frame = CGRectMake(middleBtn.right, 0, 74, 90);
        [rigthBtn setTitle:@"身份变更" forState:UIControlStateNormal];
        [rigthBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        rigthBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//        [rigthBtn setBackgroundColor:[UIColor colorWithHexString:@"0c8ada"]];
        [rigthBtn setBackgroundColor:lanse5];
        [rigthBtn addTarget:self action:@selector(rigthBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        rigthBtn.alpha = 0.88;
        [self.moreView addSubview:rigthBtn];

    } else if ([model.status isEqualToString:@"N"]) { // 停用
        
        self.moreImage.hidden  = NO;
        self.bigIV.hidden = NO;
        self.statusBtn.hidden = YES;
        self.stopLabel.hidden = NO;
        self.identityLabel.backgroundColor = [UIColor colorWithHexString:@"cccccc"];
        self.statusLabel.text = @"";
        
        UIView *moreView = [[UIView alloc] init];
        moreView.backgroundColor = [UIColor clearColor]; // [UIColor colorWithWhite:0.f alpha:0.8]
        moreView.frame = CGRectMake(ScreenWidth, 0, 74*1, self.height);
        self.moreView = moreView;
        [self addSubview:moreView];
        
        // 左边按钮
        UIButton *leftBtn = [[UIButton alloc] init];
        leftBtn.frame = CGRectMake(0, 0, 74, 90);
        [leftBtn setTitle:@"启用" forState:UIControlStateNormal];
        [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        leftBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//        [leftBtn setBackgroundColor:[UIColor colorWithHexString:@"1294e6"]];
        [leftBtn setBackgroundColor:lanse4];
        [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        leftBtn.alpha = 0.88;
        
        //    leftBtn
        [self.moreView addSubview:leftBtn];
        
    }
    
    
    
    
//    // 左边按钮
//    UIButton *leftBtn = [[UIButton alloc] init];
//    leftBtn.frame = CGRectMake(0, 0, 74, 90);
//    [leftBtn setTitle:@"启用" forState:UIControlStateNormal];
//    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    leftBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//    [leftBtn setBackgroundColor:[UIColor colorWithHexString:@"1294e6"]];
//    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    leftBtn.alpha = 0.88;
//
//    //    leftBtn
//    [self.moreView addSubview:leftBtn];
//
//
//    // 中间按钮
//    UIButton *middleBtn = [[UIButton alloc] init];
//    middleBtn.frame = CGRectMake(leftBtn.right, 0, 74, 90);
//    [middleBtn setTitle:@"停用" forState:UIControlStateNormal];
//    [middleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    middleBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//    [middleBtn setBackgroundColor:[UIColor colorWithHexString:@"0c8ada"]];
//    [middleBtn addTarget:self action:@selector(middleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    middleBtn.alpha = 0.88;
//    [self.moreView addSubview:middleBtn];
//
//    // 右边按钮
//    UIButton *rigthBtn = [[UIButton alloc] init];
//    rigthBtn.frame = CGRectMake(middleBtn.right, 0, 74, 90);
//    [rigthBtn setTitle:@"身份变更" forState:UIControlStateNormal];
//    [rigthBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    rigthBtn.titleLabel.font = [UIFont systemFontOfSize:14.0];
//    [rigthBtn setBackgroundColor:[UIColor colorWithHexString:@"0880c6"]];
//    [rigthBtn addTarget:self action:@selector(rigthBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    rigthBtn.alpha = 0.88;
//    [self.moreView addSubview:rigthBtn];
    
}

- (void)awakeFromNib {
    [super awakeFromNib];
    self.iconImageView.layer.cornerRadius = 6.0;
    self.iconImageView.layer.masksToBounds = YES;
    self.stopLabel.layer.cornerRadius = 6.0;
    self.stopLabel.layer.masksToBounds = YES;
    
    self.statusBtn.layer.borderColor = [UIColor colorWithHexString:@"ff3300"].CGColor;
    self.statusBtn.layer.borderWidth = 1.0;
    self.statusBtn.layer.cornerRadius = 2.0;
    self.statusBtn.layer.masksToBounds = YES;
    self.statusLabel.textColor = [UIColor colorWithHexString:@"ff3300"];
    
    UIImageView *iv = [[UIImageView alloc] init];
    self.moreImage = iv;
    [iv setImage:[UIImage imageNamed:@"more"]];
//    iv.userInteractionEnabled = YES;
    [self addSubview:iv];
    
    UIImageView *bigIV = [[UIImageView alloc] init];
    self.bigIV = bigIV;
    bigIV.backgroundColor = [UIColor clearColor];
    bigIV.userInteractionEnabled = YES;
    [self addSubview:bigIV];
    
    UITapGestureRecognizer * PrivateLetterTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAvatarView:)];
    PrivateLetterTap.numberOfTouchesRequired = 1; //手指数
    PrivateLetterTap.numberOfTapsRequired = 1; //tap次数
    PrivateLetterTap.delegate= self;
//    [iv addGestureRecognizer:PrivateLetterTap];
    [bigIV addGestureRecognizer:PrivateLetterTap];

}

// 点击左边的按钮
- (void)leftBtnClick:(UIButton *)btn{
//    DLog(@"left----");
    if ([self.delegate respondsToSelector:@selector(leftBtnClick:)]) {
        [self.delegate leftBtnClick:self.index];
    }
    [self moreViewHide];
}

// 点击右边的按钮
- (void)rigthBtnClick:(UIButton *)btn{
//    DLog(@"rigth----");
    if ([self.delegate respondsToSelector:@selector(rightBtnClick:)]) {
        [self.delegate rightBtnClick:self.index];
    }
    [self moreViewHide];
}

// 点击中间的按钮
- (void)middleBtnClick:(UIButton *)btn{
//    DLog(@"middle----");
    if ([self.delegate respondsToSelector:@selector(leftBtnClick:)]) {
        [self.delegate middleBtnClick:self.index];
    }
    [self moreViewHide];
}

// 显示无动画
- (void)moreViewShowWithNoAnimate{
    self.moreView.hidden = NO;
    
    NSLog(@"%ld", self.moreView.subviews.count);
    
    
    self.moreView.frame = CGRectMake(ScreenWidth - 74*3, 0, 74*3, self.height);
}

// 显示有动画效果
- (void)moreViewShow{
    self.moreView.hidden = NO;
    
     NSLog(@"%ld", self.moreView.subviews.count);
    
    if (self.moreView.subviews.count == 2) {
        [UIView animateWithDuration:0.5 animations:^{
            self.moreView.frame = CGRectMake(ScreenWidth - 74*2, 0, 68*2, self.height);
        }];
    } else if (self.moreView.subviews.count == 1){
        [UIView animateWithDuration:0.5 animations:^{
            self.moreView.frame = CGRectMake(ScreenWidth - 74*1, 0, 68*1, self.height);
        }];
    }
    
}

// 不显示
- (void)moreViewHide{
    [UIView animateWithDuration:0.25 animations:^{
        self.moreView.frame = CGRectMake(ScreenWidth, 0, 74*3, self.height);
    }];
}

// 点击更多
- (void)tapAvatarView: (UITapGestureRecognizer *)gesture {

    if ([self.delegate respondsToSelector:@selector(clickMoreBtn:)]) {
        [self.delegate clickMoreBtn:self.index];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

// 去审核身份状态
- (IBAction)gotoCheckIdentity:(UIButton *)sender {
    
    if ([self.delegate respondsToSelector:@selector(gotoCheckIdentity:)]) {
        [self.delegate gotoCheckIdentity:self.index];
    }

}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.identityLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.identityStrSize.width + 2);
        make.height.mas_equalTo(self.identityStrSize.height + 0);
    }];
    
    [self.moreImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-22);
        make.centerY.equalTo(self.statusBtn.mas_centerY);
        make.width.equalTo(@18);
        make.height.equalTo(@18);
    }];
    
    [self.bigIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(0);
        make.centerY.equalTo(self.statusBtn.mas_centerY);
        make.width.equalTo(@90);
        make.height.equalTo(@90);
    }];
}

@end
